USE [Dosvid]
GO

/****** Object:  Trigger [dbo].[Vacancy_CommodityGroups_ITrig]    Script Date: 09/10/2011 23:50:51 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


create TRIGGER [dbo].[Vacancy_Territories_ITrig] ON [dbo].Vacancy_Territories AFTER INSERT
AS 
BEGIN
  if @@rowcount = 0 return	 

  update v set
    v.DLC = convert(datetime,convert(varchar,getdate(),102))
  from Vacancies v
  join inserted i on v.Id = i.VacancyID
  where isnull(v.DLC,dateadd(day,-1,getdate())) <> convert(datetime,convert(varchar,getdate(),102))
END
GO


create TRIGGER [dbo].[Vacancy_Territories_UTrig] ON [dbo].Vacancy_Territories AFTER UPDATE
AS 
BEGIN
  if @@rowcount = 0 return	 

  update v set
    v.DLC = convert(datetime,convert(varchar,getdate(),102))
  from Vacancies v
  join inserted i on v.Id = i.VacancyID
  join deleted d on d.Id = i.Id
  where isnull(d.TerritoryId,0) <> isnull(i.TerritoryId,0) and
        isnull(v.DLC,dateadd(day,-1,getdate())) <> convert(datetime,convert(varchar,getdate(),102))
END
GO


create TRIGGER [dbo].[Vacancy_Territories_DTrig] ON [dbo].Vacancy_Territories AFTER DELETE
AS 
BEGIN
  if @@rowcount = 0 return	 

  update v set
    v.DLC = convert(datetime,convert(varchar,getdate(),102))
  from Vacancies v
  join deleted d on v.Id = d.VacancyID
  where isnull(v.DLC,dateadd(day,-1,getdate())) <> convert(datetime,convert(varchar,getdate(),102))
END



